import { Login } from '../../request/index';
import storage from '../../utils/storage'

export const LoginTypes = {
  TOGGLE_Login: "toggle_login",
  INIT_PAGERENDER: 'init_pageRender',
  GETUSERINFO: 'getuserinfo'
};

const state = {
  pageRender: false,
  token: localStorage.getItem('token') || null,
  userinfo: {}
};


const actions = {
  async login({ commit }: any, model: any) {
    await Login(model).then((res:any) => {
      console.log(res.data)
      if (res.data.code == 0) {
        commit(LoginTypes.TOGGLE_Login, res.data);
        commit(LoginTypes.INIT_PAGERENDER)
      }
    });
  }

};
const tokenStroageKe = 'token';

const mutations = {
  [LoginTypes.TOGGLE_Login](state: any,
    value: any
  ) {
    state.token = value.data.token;
    state.userinfo = value.data.user
    storage.set('user', state.userinfo);
    storage.set(tokenStroageKe, state.token);
  },
  [LoginTypes.INIT_PAGERENDER](state: any) {
    state.pageRender = true;
  },
  [LoginTypes.GETUSERINFO](state: any) {
    state.userinfo = storage.get('user');
  }
};
const getters = {
  token: (state: any) => state.token,
};
export default {
  namespaced: true,
  state,
  getters,
  actions,
  mutations,
  LoginTypes
};

